using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;

using Reflector.CodeModel;
using Reflector;
using System.Xml.Serialization;
using System.Xml;
using XMIAddin.ReflectorLoad;

namespace XMIAddin
{
    public class XMIExporter: IPackage
    {
        private IAssemblyBrowser assemblyBrowser;
        private ICommandBarManager commandBarManager = null;
        private ICommandBarSeparator separator = null;
        private ICommandBarButton button = null;
        private ICommandBarSeparator toolSeparator = null;
        private ICommandBarButton toolButton = null;

        #region IPackage Members

        public void Load(IServiceProvider serviceProvider)
        {
            assemblyBrowser = (IAssemblyBrowser)serviceProvider.GetService(typeof(IAssemblyBrowser));
            ILanguageManager languageManager = (ILanguageManager)serviceProvider.GetService(typeof(ILanguageManager));
            ITranslatorManager translatorManager = (ITranslatorManager)serviceProvider.GetService(typeof(ITranslatorManager));
            IVisibilityConfiguration visibilityConfiguration = (IVisibilityConfiguration)serviceProvider.GetService(typeof(IVisibilityConfiguration));

            this.commandBarManager = (ICommandBarManager)serviceProvider.GetService(typeof(ICommandBarManager));            

            this.separator = commandBarManager.CommandBars["Tools"].Items.AddSeparator();
            this.button = commandBarManager.CommandBars["Tools"].Items.AddButton("&XMI Export", new EventHandler(this.ExportXMIClicked));
            button.Image = XMIAddin.Properties.Resources.Logo;

            this.toolSeparator = commandBarManager.CommandBars["ToolBar"].Items.AddSeparator();
            this.toolButton = commandBarManager.CommandBars["ToolBar"].Items.AddButton("Xmi Export", XMIAddin.Properties.Resources.Logo, new EventHandler(this.ExportXMIClicked));

            assemblyBrowser.ActiveItemChanged += new EventHandler(AssemblyBrowserActiveItemChanged);
            //button.Image = 

            RedoButtonEnabled();
        }

        public void Unload()
        {
            this.commandBarManager.CommandBars["Tools"].Items.Remove(this.button);
            this.commandBarManager.CommandBars["Tools"].Items.Remove(this.separator);
            this.commandBarManager.CommandBars["Tools"].Items.Remove(this.toolButton);
            this.commandBarManager.CommandBars["Tools"].Items.Remove(this.toolSeparator);
        }

        #endregion

        void RedoButtonEnabled()
        {
            this.button.Enabled = assemblyBrowser.ActiveItem is IAssembly;
            this.toolButton.Enabled = this.button.Enabled;
        }

        void AssemblyBrowserActiveItemChanged(object sender, EventArgs e)
        {
            RedoButtonEnabled();
        }

        private void ExportXMIClicked(object sender, EventArgs e)
        {
            if (false == assemblyBrowser.ActiveItem is IAssembly) return;

            SaveFileDialog sfd = new SaveFileDialog();
            using (sfd)
            {
                sfd.AddExtension = true;
                sfd.Filter = "XMI File (*.xmi)|*.xmi|All Files (*.*)|*.*";
                sfd.DefaultExt = ".xmi";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    string filename = sfd.FileName;
                    SaveTo(filename);
                }
            }
        }

        private void SaveTo(string filename)
        {
            Xmi.XmiDocument doc = new XMIAddin.Xmi.XmiDocument();
            XmlSerializer ser = new XmlSerializer(typeof(Xmi.XmiDocument));
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF7;
            settings.Indent = true;
            settings.IndentChars = "  ";
            settings.ConformanceLevel = ConformanceLevel.Document;
            settings.NewLineChars = "\r\n";
            settings.NewLineHandling = NewLineHandling.Replace;
            settings.NewLineOnAttributes = false;
            XmlWriter writer = XmlWriter.Create(filename);
            using (writer)
            {
                IAssembly assembly = assemblyBrowser.ActiveItem as IAssembly;
                doc.Content.Model = new XMIAddin.Xmi.UmlModel();
                doc.Content.Model.Name = "Model of " + assembly.Name + " version " + assembly.Version;
                
                ModelLoader loader = new ModelLoader(doc.Content.Model);
                loader.Load(assemblyBrowser.ActiveItem as IAssembly);

                ser.Serialize(writer, doc); //, new XmlSerializerNamespaces(), "UTF-8");
            }
        }
    }
}
